
import os
from typing import List,Dict
import datetime


from openpyxl import Workbook



def writeExcel(path: str,fileName: str,data: List[Dict]):
    
    if not data or len(data) == 0:
        return None
    
    wb = Workbook()
    ws = wb.active
    
    if not os.path.exists(path):
        os.mkdir(path)
    
    date = datetime.datetime.now()
    
    fileName = "-".join([date.strftime("%Y%m%d"),fileName,date.strftime("%f")]) + ".xlsx"
    
    finalFile = os.path.join(path,fileName)
    
    i = 1
    j = 1
    
    header = [ item for item in data[0].keys()]
    
    print(header)
    for item in header:
        _ = ws.cell(row=i,column=j,value=item)
        j += 1
        
    i += 1
    j = 1
    for item in data:
        for row in item.values():
            _ = ws.cell(row=i,column=j,value=row)
            j += 1
        i += 1
        j = 1
    
    wb.save(filename = finalFile)
    
    return finalFile,fileName


if __name__ == "__main__":
    
    data = [{"title1":"asd","title2":"zxc"},{"title1":"123","title2":"qwe"}]
    
    print(writeExcel("D:\\temp\\temp","test",data))